package PageUtils

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Number"
	"strconv"
)

func MakeSelectCountSQL(tableName string, filter string) string {
	sb := ""

	where := ""
	if filter != "" {
		where = "WHERE (" + filter + ")"
	}

	sb += "        SELECT count(1)                          "
	sb += "        FROM " + tableName + " t1                "
	sb += where

	return sb
}

func MakeSelectSQLPage(tableName string, vFields []string, cFields []string, filter string, order string, pageNmu int, pageSize int) string {
	sb := ""

	pageIndex := (pageNmu - 1) * pageSize

	where := ""
	if filter != "" {
		where = "WHERE (" + filter + ")"
	}

	ASC := "ASC"
	//DESC := "DESC"
	if order == "DESC" {
		ASC = "DESC"
		//DESC = "ASC"
	}

	sbf := ""
	if Method.HasEmpty(vFields, cFields) {
		sbf += "t4.* ,"
	}
	if !Method.HasEmpty(vFields) {
		for _, field := range vFields {
			sbf += "t4."
			sbf += field
			sbf += ","
		}
	}
	if !Method.HasEmpty(cFields) {
		for _, field := range vFields {
			sbf += field
			sbf += ","
		}
	}

	fs := sbf[0 : len(sbf)-1]

	sb += "        SELECT " + fs + "                        "
	sb += "        FROM " + tableName + " t4                "
	sb += "        RIGHT JOIN                               "
	sb += "        (                                        "
	sb += "                SELECT t1.id                     "
	sb += "        FROM " + tableName + " t1                "
	sb += where
	sb += "        ORDER BY t1.id " + ASC + "               "
	sb += "        LIMIT " + strconv.Itoa(pageIndex) + "," + strconv.Itoa(pageSize) + " "
	sb += "             ) t3                                "
	sb += "        ON t4.id = t3.id                         "
	sb += "        ORDER BY t4.id " + ASC + "               "

	return sb
}

func GetPageList(entityList []commEntity.IEntity, body map[string]interface{}) map[string]interface{} {
	data := make(map[string]interface{})
	data["total"] = len(entityList)

	// 根据ID排序
	commEntity.SortEntityList(entityList)

	if Map.Has(body, "pageNum") && Map.Has(body, "pageSize") {
		resultList := make([]commEntity.IEntity, 0)

		pageNum := Number.MakeInt(body["pageNum"])
		pageSize := Number.MakeInt(body["pageSize"])
		pageStartId := pageSize * (pageNum - 1)
		pageEndId := pageSize * pageNum
		index := 0

		for _, entity := range entityList {
			if index < pageStartId {
				index++
				continue
			}
			if index >= pageEndId {
				break
			}

			resultList = append(resultList, entity)
			index++
		}

		data["list"] = commEntity.BuildMapList(resultList)
	} else {
		data["list"] = commEntity.BuildMapList(entityList)
	}

	return data
}

func GetMapPageList(entityList []map[string]interface{}, pageNum, pageSize int) map[string]interface{} {
	data := make(map[string]interface{})
	data["total"] = len(entityList)

	resultList := make([]map[string]interface{}, 0)

	pageStartId := pageSize * (pageNum - 1)
	pageEndId := pageSize * pageNum
	index := 0

	for _, entity := range entityList {
		if index < pageStartId {
			index++
			continue
		}
		if index >= pageEndId {
			break
		}

		resultList = append(resultList, entity)
		index++
	}

	data["list"] = resultList

	return data
}
